package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.timmy.tdialog.TDialog;
import com.timmy.tdialog.base.BindViewHolder;
import com.timmy.tdialog.listener.OnBindViewListener;
import com.timmy.tdialog.listener.OnViewClickListener;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.manager.CustomDialogManager;
import com.ycsj.common.manager.TDialogManager;
import com.ycsj.common.utils.MaxTextLengthFilter;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.Res;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.teacher.adapter.ClassGradeGvAdapter;
import com.ycsj.goldmedalnewconcept.teacher.bean.ClassInfo;
import com.ycsj.goldmedalnewconcept.teacher.utils.ClassUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的班级
 * Created by Administrator on 2019/3/22 0022.
 */

public class TeacherMyClassAndGradeActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.gv)
    GridView gv;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //数据源
    private List<ClassInfo.ListBean> oldDatas;

    //适配器
    private ClassGradeGvAdapter adapter;

    //界面刷新的标识
    public static final int UPDATEPAGE = 0x880;


    //班级的头像数组
    private int classHeads[] = {R.drawable.class1, R.drawable.class2, R.drawable.class3, R.drawable.class4, R.drawable.class5, R.drawable.class6,
            R.drawable.class7, R.drawable.class8, R.drawable.class9, R.drawable.class10};

    @Override
    protected int getContentLayout() {
        return R.layout.activity_teacher_class_grade;
    }

    @Override
    protected void init() {
        //沉浸状态栏处理
        initStateBar();

        //初始化标题栏
        initTitle();

        //请求班级数据
        http();
    }

    /**
     * 初始化标题
     */
    private void initTitle() {
        tvTitle.setText("我的班级");
    }


    /**
     * 请求班级数据
     */
    private void http() {
        ClassUtil.httpClass(rlLoading, this, new ClassUtil.onSuccessListener() {
            @Override
            public void onSuccess(List<ClassInfo.ListBean> datas) {
                rlLoading.setVisibility(View.GONE);

                //有班级数据
                oldDatas = datas;

                //设置GridView的数据
                initGv();
            }

            @Override
            public void hasNoData() {
                //没有班级数据
                oldDatas = new ArrayList<>();

                //设置GridView的数据
                initGv();
            }
        });
    }

    /**
     * 设置Gv的数据
     */
    private void initGv() {
        adapter = new ClassGradeGvAdapter(this, oldDatas);
        gv.setAdapter(adapter);

        adapter.setOnTvClickListener(new ClassGradeGvAdapter.OnTvClickListener() {
            @Override
            public void addClass() {
                //添加班级的弹框
                showAddClassPop();
            }

            @Override
            public void goClassInfo(ClassInfo.ListBean listBean) {
                //跳转到班级信息
                TeacherClassInfoActivity.goActivity(TeacherMyClassAndGradeActivity.this, listBean);
            }
        });
    }

    /**
     * 添加班级的弹框
     */
    private void showAddClassPop() {
        TDialogManager.show(this, getSupportFragmentManager(), R.layout.pop_add_class, 0.3f, new OnBindViewListener() {
            @Override
            public void bindView(BindViewHolder viewHolder) {
                //班级头像
                ImageView lvClass = viewHolder.getView(R.id.lv_class);
                lvClass.setImageResource(classHeads[oldDatas.size()]);
                EditText etInput = viewHolder.getView(R.id.et_input);
                etInput.setFilters(new InputFilter[]{new MaxTextLengthFilter(11)});
            }
        }, new OnViewClickListener() {
            @Override
            public void onViewClick(BindViewHolder viewHolder, View view, TDialog tDialog) {
                switch (view.getId()) {
                    case R.id.bt_save:
                        EditText etInput = viewHolder.getView(R.id.et_input);
                        String className = etInput.getText().toString().trim();
                        if (TextUtils.isEmpty(className)) {
                            ToastUtils.showShort("未输入班级名称");
                            return;
                        }
                        //请求服务器添加班级,添加成功后刷新界面
                        doCreateClassAction(className);
                        break;
                    case R.id.lv_close:
                        //关闭弹框
                        TDialogManager.dismiss();
                        break;
                }
            }
        }, R.id.bt_save, R.id.lv_close);
    }


    /**
     * 修改班级名称
     *
     * @param className
     */
    private void doCreateClassAction(String className) {
        TDialogManager.dismiss();
        CustomDialogManager.show(this, "创建中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "3");
        map.put("userID", AccountUtils.getAccount(this));
        map.put("icon", (oldDatas.size() + 1) + "");
        map.put("name", className);

        HttpUtils.post(HttpAddress.CREATECLASS, map, this, new HttpCallBack<Res>() {
            @Override
            public void onSuccess(Res res) {
                if (1 == res.getState()) {
                    //修改成功
                    ToastUtils.showShort("创建成功");
                    //通知本页面刷新数据
                    //通知TeacherMyClassAndGradActivity刷新
                    BusInfo busInfo = new BusInfo();
                    busInfo.type = TeacherMyClassAndGradeActivity.UPDATEPAGE;
                    EventBus.getDefault().post(busInfo);
                    //更新页面的班级名称
                    CustomDialogManager.dissmiss();
                }
            }

            @Override
            public void onError(String e) {
                ToastUtils.showShort(BaseApplication.ERROR);
                CustomDialogManager.dissmiss();
            }
        });

    }


    /**
     * 点击事件处理
     */
    @OnClick({R.id.iv_back})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
        }
    }

    /**
     * 事件处理
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        if (null != busInfo && UPDATEPAGE == busInfo.type) {
            //重新请求班级数据
            ClassUtil.httpClass(rlLoading, this, new ClassUtil.onSuccessListener() {
                @Override
                public void onSuccess(List<ClassInfo.ListBean> datas) {
                    oldDatas.clear();
                    oldDatas.addAll(datas);
                    //适配器刷新
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void hasNoData() {
                }
            });
        }
        super.busEvent(busInfo);
    }
}
